package com.ying.rabbitmqspringbootproducer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * ClassName: DLXConfig
 * Package: com.ying.rabbitmqspringbootproducer.config
 * Description:
 *
 * @Author 刘英杰
 * @Create 2025/7/26 17:25
 * @Version 1.0
 */

@Configuration
public class DLXConfig {

    public static final String EXCHANGE_NORMAL_NAME = "normalExchange";
    public static final String QUEUE_NORMAL_NAME = "normalQueue";

    public static final String EXCHANGE_DLX_NAME = "dlxExchange";
    public static final String QUEUE_DL_NAME = "dlQueue";


    //普通
    @Bean
    public Exchange normalExchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_NORMAL_NAME).durable(true).build();
    }

    @Bean
    public Queue normalQueue() {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("x-dead-letter-exchange", EXCHANGE_DLX_NAME);
        params.put("x-dead-letter-routing-key","order.status.error");

//        params.put("x-max-length",5);
//        params.put("x-message-ttl",10000);

        return QueueBuilder.durable(QUEUE_NORMAL_NAME).withArguments(params).build();
    }

    @Bean
    public Binding bindingNormalQueue(Exchange normalExchange, Queue normalQueue) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("order.#").noargs();
    }


    //死信
    @Bean
    public Exchange dlxExchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_DLX_NAME).durable(true).build();
    }


    @Bean
    public Queue dlQueue() {
        return QueueBuilder.durable(QUEUE_DL_NAME).build();
    }

    @Bean
    public Binding bindingDlQueue(Exchange dlxExchange, Queue dlQueue) {
        return BindingBuilder.bind(dlQueue).to(dlxExchange).with("order.status.#").noargs();
    }
}
